package com.enseirb.bdxmap.dao.parser;

import org.json.simple.JSONObject;

import com.enseirb.bdxmap.dao.dto.ParkingMarkerDTO;

public class ParkingMarkerParser extends AbstractMarkerParser<ParkingMarkerDTO> {
	private static final String JSON_KEY_NB_SPOTS = "nombre_de_places";
	private static final String JSON_KEY_TYPE = "nature";
	private static final String JSON_KEY_NAME = "nom";
	private static final String JSON_KEY_LAT = "y_lat";
	private static final String JSON_KEY_LONG = "x_long";
	private static final String JSON_KEY_ID = "entityid";
	private static final String DEFAULT_NAME = "Parking sans nom";
	
	public ParkingMarkerParser() {
		super();
		json_key_id = JSON_KEY_ID;
		json_key_long = JSON_KEY_LONG;
		json_key_lat = JSON_KEY_LAT;
		json_key_name = JSON_KEY_NAME;
		default_marker_name = DEFAULT_NAME;
	}
	
	@Override
	public ParkingMarkerDTO newServiceDTO() {
		return new ParkingMarkerDTO();
	}

	@Override
	protected ParkingMarkerDTO getCustomFieldsFromJson(
			ParkingMarkerDTO parkingDTO, JSONObject parkingJson) {
		parkingDTO.type = getTypeFromJson(parkingJson);
		parkingDTO.nbSpots = getNbSpotsFromJson(parkingJson);
		return parkingDTO;
	}

	private String getTypeFromJson(JSONObject parkingJson) {
		if(parkingJson.containsKey(JSON_KEY_TYPE)) {
			return parkingJson.get(JSON_KEY_TYPE).toString();
		} else {
			return "";
		}
	}

	private String getNbSpotsFromJson(JSONObject parkingJson) {
		if(parkingJson.containsKey(JSON_KEY_NB_SPOTS)) {
			return parkingJson.get(JSON_KEY_NB_SPOTS).toString();
		} else {
			return "";
		}
	}
}
